#ifndef BLOCKVERTEXBUFFERSORTER_H_
#define BLOCKVERTEXBUFFERSORTER_H_

#include "D3DFlyweight.h"
#include <vector>
#include "Minecraft/Containers/Chunk.h"

// If you change this, update Instancing.fx
// This should be a power of 2
// WARNING: Do NOT set this number too high (<= 512).  You will BLUE SCREEN your computer.
#define BUFFER_SIZE 128

// Format of the box vertices for shader instancing
struct BOX_VERTEX_INSTANCE
{
    D3DXVECTOR3 pos;    // Position of the vertex
    float u, v;  // Box UVs
	float i; // Instance
};

class BoxRenderer;

class BlockVertexBufferSorter
{
public:
	void Init();
	void Sort(Chunk const *_chunk);
	void Render(BLOCK_TYPE type);

private:
	void TypeBuilder(BLOCK_TYPE type, BOX_VERTEX_INSTANCE *boxVerts);

	static ID3DXEffect* m_pEffect;
	static IDirect3DIndexBuffer9* m_pIndexBuffer;
	static IDirect3DVertexDeclaration9* m_pVertDecl;
	static IDirect3DVertexBuffer9* vertBuffers[MAX_TYPE];
	static IDirect3DTexture9* m_pBoxTexture;          // Texture covering the box
	static int prims[MAX_TYPE];
	static int verts[MAX_TYPE];
	static D3DXHANDLE m_hTexture;
	static D3DXHANDLE m_hPositions;
	static D3DXHANDLE m_hLighting;

	int sizes[MAX_TYPE];
	std::vector<D3DXVECTOR4> blockPosList[MAX_TYPE];
	std::vector<UBYTE> dataList[MAX_TYPE];
	std::vector<float> lightList[MAX_TYPE];
	
	friend BoxRenderer;
};

#endif